package com.qianbao.qianbaobusiness.ui.cashier.databill;

import com.androidbase.utils.RxUtils;
import com.baseandroid.MyApplication;
import com.baseandroid.config.BossApi;
import com.baseandroid.config.Constant;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.DESedeEncrypt;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qianbao.qianbaobusiness.json.DayBillDetailedBean;
import com.qianbao.qianbaobusiness.json.DaySingleDetailBean;
import com.qianbao.qianbaobusiness.json.FeedBackTagBean;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hdb on 2017/4/26.
 */

public class BillsDetailsModelImpl implements BillsDetailsModel {

    private BaseCommActivity activity;
    private BillsDetailsRequest billsDetailsRequest;
    private BossApi bossApi;

    public BillsDetailsModelImpl(BaseCommActivity activity, BillsDetailsRequest billsDetailsRequest) {
        this.activity = activity;
        this.billsDetailsRequest = billsDetailsRequest;
        bossApi = MyApplication.getMyAppComponent().retrofitBossApiHelper();
    }


    @Override
    public void getQueryDayChecking(String chkAccountDate, String mCode) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("chkAccountDate", chkAccountDate);// 账单日期
        hashMap.put("mCode", mCode);// 商户代码
        bossApi.getBillDetail(Constant.encryptMap(hashMap))
                .compose(RxUtils.applySchedulersLifeCycle((BaseCommActivity) activity))
                .subscribe(new RxObserver<Data<String>>() {
                    @Override
                    public void onNext(Data<String> stringData) {
                        if (RxObserver.checkJsonCode(stringData, false)) {
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", stringData
                                        .getResult());
                                // new Gson().fromJson()
                                List<DayBillClassificationBean> bean = new Gson().fromJson(result, new TypeToken<List<DayBillClassificationBean>>() {
                                }.getType());
                                //                                Timber.e("result = " + result);
                                //                                getView().onSuccess(bean);
                                billsDetailsRequest.onSuccess(1, bean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
        //        NewOkHttpClientManager.postJson(HttpUrl.getInstance().URL1024, hashMap, activity.getClass().getName(), new NewOkHttpClientManager.ResultCallback<List<DayBillClassificationBean>>() {
        //            @Override
        //            public void onError(String msg) {
        //                if (UIApplication.isLogin == false) {
        //                    activity.reLogin();
        //                }
        //            }
        //
        //            @Override
        //            public void onResponse(List<DayBillClassificationBean> resp) {
        //                billsDetailsRequest.onSuccess(1, resp);
        //            }
        //
        //            @Override
        //            public void onFinish() {
        //
        //            }
        //        });

    }

    @Override
    public void getQueryTradeDetailPage(String chkAccountDate, String mCode, int type, int pageNum, int pageSize) {
        TradeDetailBean detailBean = new TradeDetailBean(chkAccountDate, mCode, type, pageNum, pageSize);
        /*NewOkHttpClientManager.postJson(HttpUrl.getInstance().URL1025, detailBean, activity.getClass().getName(), new NewOkHttpClientManager.ResultCallback<List<DayBillDetailedBean>>() {
            @Override
            public void onError(String msg) {
                if (UIApplication.isLogin == false) {
                    activity.reLogin();
                }
            }

            @Override
            public void onResponse(List<DayBillDetailedBean> resp) {
                billsDetailsRequest.onSuccess(2, resp);
            }

            @Override
            public void onFinish() {

            }
        });

*/
        String desedeStr = "";
        try {
            desedeStr = DESedeEncrypt.encrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", new Gson()
                    .toJson(detailBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String, String> paraMap = new HashMap<>();
        paraMap.put("context", desedeStr);


        bossApi.getQueryTradeDetailPage(paraMap)
                .compose(RxUtils.applySchedulersLifeCycle((BaseCommActivity) activity))
                .subscribe(new RxObserver<Data<String>>() {
                    @Override
                    public void onNext(Data<String> stringData) {
                        if (RxObserver.checkJsonCode(stringData, false)) {
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", stringData
                                        .getResult());
                                // new Gson().fromJson()
                                List<DayBillDetailedBean> bean = new Gson().fromJson(result, new TypeToken<List<DayBillDetailedBean>>() {
                                }.getType());
                                //                                Timber.e("result = " + result);
                                //                                getView().onSuccess(bean);
                                billsDetailsRequest.onSuccess(2, bean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    class TradeDetailBean {
        public String chkAccountDate;
        public String mCode;
        public int type;
        public int pageNum;
        public int pageSize;

        public TradeDetailBean(String chkAccountDate, String mCode, int type, int pageNum, int pageSize) {
            this.chkAccountDate = chkAccountDate;
            this.mCode = mCode;
            this.type = type;
            this.pageNum = pageNum;
            this.pageSize = pageSize;
        }
    }

    @Override
    public void getQueryTradeDetail(int settlementId) {
        TradeBean tradeBean = new TradeBean(settlementId);
        /*NewOkHttpClientManager.postJson(HttpUrl.getInstance().URL1026, tradeBean, activity.getClass().getName(), new NewOkHttpClientManager.ResultCallback<List<DaySingleDetailBean>>() {
            @Override
            public void onError(String msg) {
                if (UIApplication.isLogin == false) {
                    activity.reLogin();
                }
            }

            @Override
            public void onResponse(List<DaySingleDetailBean> resp) {
                billsDetailsRequest.onSuccess(3, resp);
            }


            @Override
            public void onFinish() {

            }
        });*/

        String desedeStr = "";
        try {
            desedeStr = DESedeEncrypt.encrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", new Gson()
                    .toJson(tradeBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String, String> paraMap = new HashMap<>();
        paraMap.put("context", desedeStr);


        bossApi.getQueryTradeDetail(paraMap)
                .compose(RxUtils.applySchedulersLifeCycle((BaseCommActivity) activity))
                .subscribe(new RxObserver<Data<String>>() {
                    @Override
                    public void onNext(Data<String> stringData) {
                        if (RxObserver.checkJsonCode(stringData, false)) {
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", stringData
                                        .getResult());
                                // new Gson().fromJson()
                                List<DaySingleDetailBean> bean = new Gson().fromJson(result, new TypeToken<List<DaySingleDetailBean>>() {
                                }.getType());
                                //                                Timber.e("result = " + result);
                                //                                getView().onSuccess(bean);
                                billsDetailsRequest.onSuccess(3, bean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getFeedBackTagList(String sceneId) {
        MyApplication.getMyAppComponent()
                .retrofitMyApiHelper()
                .getFeedbackTags(sceneId)
                .compose(RxUtils.applySchedulersLifeCycle((BaseCommActivity) activity))
                .subscribe(new RxObserver<Data<FeedBackTagBean>>() {
                    @Override
                    public void onNext(Data<FeedBackTagBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, true)) {
                            billsDetailsRequest.onSuccess(4, data.getResult());
                        }
                    }
                });
    }

    class TradeBean {
        public int settlementId;

        public TradeBean(int settlementId) {
            this.settlementId = settlementId;
        }
    }
}
